package com.internetCafes.spms.web.tenant.bizservice.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.internetCafes.spms.core.exception.RRException;
import com.internetCafes.spms.core.exception.RRExceptionCodeEnum;
import com.internetCafes.spms.web.tenant.bizservice.TenantDeptBizService;
import com.internetCafes.spms.web.tenant.common.TenantConst;
import com.internetCafes.spms.web.tenant.model.TenantDeptInfoPlus;
import com.internetCafes.spms.web.tenant.service.ITenantDeptInfoPlusService;
import com.internetCafes.spms.web.tenant.vo.TenantDeptInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 租户部门信息业务处理层
 *
 * @author HHKJ-SERVER-ZJ 2022/3/3 10:29
 */
@Service
public class TenantDeptBizServiceImpl implements TenantDeptBizService {

    @Autowired
    private ITenantDeptInfoPlusService deptInfoPlusService;

    @Override
    public boolean updateBatchByTree(TenantDeptInfoVo req) {
        if (ObjectUtil.isNull(req)) {
            throw new RRException(RRExceptionCodeEnum.PARAM_ERROR);
        }
        /*
         * 1-遍历出各个部门的父id 和排序sort 放到list中， 需要去除根节点信息
         * 2-对部门信息进行批量更新
         */
        updateParentId(req.getChildren(), TenantConst.TENANT_DEPT_ROOT_PARENT_ID);
        List<TenantDeptInfoPlus> deptUpdateDoList = new ArrayList<>();
        filterDeptUpdateInfoFromReq(deptUpdateDoList, req.getChildren());
        if (CollectionUtil.isEmpty(deptUpdateDoList)) {
            return false;
        }
        return deptInfoPlusService.updateBatchById(deptUpdateDoList);
    }

    /**
     * 跟新部门父节点id
     *
     * @param deptReqList 部门列表
     */
    private void updateParentId(List<TenantDeptInfoVo> deptReqList, Long parentId) {
        if (CollectionUtil.isEmpty(deptReqList)) {
            return;
        }
        deptReqList.forEach(deptReq -> {
            if (CollectionUtil.isNotEmpty(deptReq.getChildren())) {
                updateParentId(deptReq.getChildren(), deptReq.getId());
            }
            deptReq.setParentId(parentId);
        });
    }

    /**
     * 冲请求中过滤出需要更新的部门信息
     *
     * @param deptUpdateDoList 存放 部门 信息的结果集
     * @param deptReqList      请求的部门列表
     */
    private void filterDeptUpdateInfoFromReq(List<TenantDeptInfoPlus> deptUpdateDoList, List<TenantDeptInfoVo> deptReqList) {

        if (CollectionUtil.isEmpty(deptReqList)) {
            return;
        }
        Map<Long, Integer> sortMapByParentId = new HashMap<>();
        int index;
        for (TenantDeptInfoVo deptInfoVo : deptReqList) {
            if (sortMapByParentId.containsKey(deptInfoVo.getParentId())) {
                index = sortMapByParentId.get(deptInfoVo.getParentId());
                deptInfoVo.setSort(++index);
                sortMapByParentId.put(deptInfoVo.getParentId(), index);
            } else {
                index = 1;
                sortMapByParentId.put(deptInfoVo.getParentId(), index);
                deptInfoVo.setSort(index);
            }
        }

        List<TenantDeptInfoPlus> deptDoList = deptReqList
                .stream()
                .map(req ->
                        new TenantDeptInfoPlus()
                                .setId(req.getId())
                                .setParentId(req.getParentId())
                                .setSort(req.getSort()))
                .collect(Collectors.toList());
        deptUpdateDoList.addAll(deptDoList);

        // 获取子部门信息
        deptReqList = deptReqList.stream().map(TenantDeptInfoVo::getChildren).flatMap(Collection::stream).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(deptReqList)) {
            filterDeptUpdateInfoFromReq(deptUpdateDoList, deptReqList);
        }
    }
}
